package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    CheckGroupDao checkGroupDao;
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.add(checkGroup);
        if (checkitemIds.length != 0) {
            Integer checkGroupId = checkGroup.getId();
            this.addCheckGroupRelationCheckitem(checkitemIds, checkGroupId);
        }
    }

    @Override
    public PageResult findByPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckGroup> checkGroups = checkGroupDao.selectByCondition(queryString);
        List<CheckGroup> result = checkGroups.getResult();
        long total = checkGroups.getTotal();
        return new PageResult(total,result);
    }

    @Override
    public CheckGroup findCheckGroupById(Integer groupById) {
        return checkGroupDao.findCheckGroupById(groupById);

    }

    @Override
    public List<Integer> findCheckItemsByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemsByCheckGroupId(id);
    }

    @Override
    public void editCheckGroupAndCheckGroupReCheckitem(Integer[] checkItemIds, CheckGroup checkGroup) {
        checkGroupDao.editCheckGroup(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        //从中间表删除检查组id对应的所有检查项
        checkGroupDao.deleteByCheckGroupId(checkGroupId);
        //在中间表中添加关联信息
        addCheckGroupRelationCheckitem(checkItemIds, checkGroupId);
    }

    private void addCheckGroupRelationCheckitem(Integer[] checkItemIds, Integer checkGroupId) {
        for (Integer checkItemId : checkItemIds) {
            HashMap<String, Integer> map = new HashMap<>();
            map.put("checkGroupId", checkGroupId);
            map.put("checkItemId",checkItemId);
            checkGroupDao.addCheckGroupReCheckitem(map);
        }
    }

    @Override
    public void deleteByCheckGroupId(Integer id) {
        List<Integer> checkItems = checkGroupDao.findCheckItemsByCheckGroupId(id);
        if (checkItems.size() > 0){
            throw new RuntimeException("检查组中不为空,无法删除");
        }
        checkGroupDao.deleteCheckGroup(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();

    }
}
